/*     */ package com.zimbra.cs.index;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Task;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class TaskHit
/*     */   extends CalendarItemHit
/*     */ {
/*     */   TaskHit(ZimbraQueryResultsImpl results, Mailbox mbx, int id, Task task, Object sortValue)
/*     */   {
/*  29 */     super(results, mbx, id, task, sortValue);
/*     */   }
/*     */   
/*     */   public long getDueTime() throws ServiceException {
/*  33 */     Task task = (Task)getCalendarItem();
/*  34 */     return task.getEndTime();
/*     */   }
/*     */   
/*     */   public int getCompletionPercentage() throws ServiceException {
/*  38 */     Task task = (Task)getCalendarItem();
/*  39 */     Invite inv = task.getDefaultInviteOrNull();
/*  40 */     if (inv != null) {
/*  41 */       String compPerc = inv.getPercentComplete();
/*  42 */       if (compPerc != null) {
/*     */         try {
/*  44 */           return Integer.parseInt(compPerc);
/*     */         }
/*     */         catch (Exception e) {
/*  47 */           return 0;
/*     */         }
/*     */       }
/*     */     }
/*  51 */     return 0;
/*     */   }
/*     */   
/*     */ 
/*     */   static enum Status
/*     */   {
/*  57 */     NEED(0), 
/*  58 */     INPR(1), 
/*  59 */     WAITING(2), 
/*  60 */     DEFERRED(3), 
/*  61 */     COMP(4);
/*     */     
/*     */ 
/*  64 */     private Status(int sortVal) { this.mSortVal = sortVal; }
/*  65 */     public int getSortVal() { return this.mSortVal; }
/*     */     
/*     */     private int mSortVal;
/*     */   }
/*     */   
/*  70 */   public Status getStatus() throws ServiceException { Task task = (Task)getCalendarItem();
/*  71 */     Invite inv = task.getDefaultInviteOrNull();
/*  72 */     if (inv != null) {
/*  73 */       String status = inv.getStatus();
/*     */       try {
/*  75 */         return Status.valueOf(status.toUpperCase());
/*     */       }
/*     */       catch (IllegalArgumentException e) {
/*  78 */         ZimbraLog.index.debug("Unknown Task Status value: " + status.toUpperCase());
/*     */       }
/*     */     }
/*  81 */     return Status.DEFERRED;
/*     */   }
/*     */   
/*     */   public Object getSortField(SortBy sort) throws ServiceException
/*     */   {
/*  86 */     switch (sort) {
/*     */     case TASK_DUE_ASC: 
/*     */     case TASK_DUE_DESC: 
/*  89 */       return Long.valueOf(getDueTime());
/*     */     case TASK_STATUS_ASC: 
/*     */     case TASK_STATUS_DESC: 
/*  92 */       return getStatus();
/*     */     case TASK_PERCENT_COMPLETE_ASC: 
/*     */     case TASK_PERCENT_COMPLETE_DESC: 
/*  95 */       return Integer.valueOf(getCompletionPercentage());
/*     */     }
/*  97 */     return super.getSortField(sort);
/*     */   }
/*     */   
/*     */   int compareTo(SortBy sort, ZimbraHit other)
/*     */     throws ServiceException
/*     */   {
/* 103 */     switch (sort) {
/*     */     case TASK_DUE_ASC: 
/* 105 */       return compareByDueDate(true, this, other);
/*     */     case TASK_DUE_DESC: 
/* 107 */       return compareByDueDate(false, this, other);
/*     */     case TASK_STATUS_ASC: 
/* 109 */       return compareByStatus(true, this, other);
/*     */     case TASK_STATUS_DESC: 
/* 111 */       return compareByStatus(false, this, other);
/*     */     case TASK_PERCENT_COMPLETE_ASC: 
/* 113 */       return compareByCompletionPercent(true, this, other);
/*     */     case TASK_PERCENT_COMPLETE_DESC: 
/* 115 */       return compareByCompletionPercent(false, this, other);
/*     */     }
/* 117 */     return super.compareTo(sort, other);
/*     */   }
/*     */   
/*     */   private static long getDueTime(ZimbraHit zh) throws ServiceException
/*     */   {
/* 122 */     if ((zh instanceof ProxiedHit)) {
/* 123 */       return ((ProxiedHit)zh).getElement().getAttributeLong("dueDate");
/*     */     }
/* 125 */     return ((TaskHit)zh).getDueTime();
/*     */   }
/*     */   
/*     */   private static Status getStatus(ZimbraHit zh) throws ServiceException {
/* 129 */     if ((zh instanceof ProxiedHit)) {
/* 130 */       String s = ((ProxiedHit)zh).getElement().getAttribute("status");
/* 131 */       return Status.valueOf(s);
/*     */     }
/* 133 */     return ((TaskHit)zh).getStatus();
/*     */   }
/*     */   
/*     */   static int getCompletionPercentage(ZimbraHit zh) throws ServiceException
/*     */   {
/* 138 */     if ((zh instanceof ProxiedHit)) {
/* 139 */       return (int)((ProxiedHit)zh).getElement().getAttributeLong("percentComplete");
/*     */     }
/* 141 */     return ((TaskHit)zh).getCompletionPercentage();
/*     */   }
/*     */   
/*     */   static final int compareByDueDate(boolean ascending, ZimbraHit lhs, ZimbraHit rhs) {
/* 145 */     int retVal = 0;
/*     */     try {
/* 147 */       long left = getDueTime(lhs);
/* 148 */       long right = getDueTime(rhs);
/* 149 */       long result = right - left;
/* 150 */       if (result > 0L) {
/* 151 */         retVal = 1;
/* 152 */       } else if (result < 0L) {
/* 153 */         retVal = -1;
/*     */       } else
/* 155 */         retVal = 0;
/*     */     } catch (ServiceException e) {
/* 157 */       ZimbraLog.index.info("Caught ServiceException trying to compare TaskHit %s to TaskHit %s", lhs, rhs, e);
/*     */     }
/*     */     
/* 160 */     if (ascending) {
/* 161 */       return -1 * retVal;
/*     */     }
/* 163 */     return retVal;
/*     */   }
/*     */   
/*     */   static final int compareByStatus(boolean ascending, ZimbraHit lhs, ZimbraHit rhs) {
/* 167 */     int retVal = 0;
/*     */     try {
/* 169 */       Status left = getStatus(lhs);
/* 170 */       Status right = getStatus(rhs);
/* 171 */       int result = right.getSortVal() - left.getSortVal();
/* 172 */       if (result > 0) {
/* 173 */         retVal = 1;
/* 174 */       } else if (result < 0) {
/* 175 */         retVal = -1;
/*     */       } else
/* 177 */         retVal = 0;
/*     */     } catch (ServiceException e) {
/* 179 */       ZimbraLog.index.info("Caught ServiceException trying to compare TaskHit %s to TaskHit %s", lhs, rhs, e);
/*     */     }
/*     */     
/* 182 */     if (ascending) {
/* 183 */       return -1 * retVal;
/*     */     }
/* 185 */     return retVal;
/*     */   }
/*     */   
/*     */   static final int compareByCompletionPercent(boolean ascending, ZimbraHit lhs, ZimbraHit rhs) {
/* 189 */     int retVal = 0;
/*     */     try {
/* 191 */       int left = getCompletionPercentage(lhs);
/* 192 */       int right = getCompletionPercentage(rhs);
/* 193 */       int result = right - left;
/* 194 */       if (result > 0) {
/* 195 */         retVal = 1;
/* 196 */       } else if (result < 0) {
/* 197 */         retVal = -1;
/*     */       } else
/* 199 */         retVal = 0;
/*     */     } catch (ServiceException e) {
/* 201 */       ZimbraLog.index.info("Caught ServiceException trying to compare TaskHit %s to TaskHit %s", lhs, rhs, e);
/*     */     }
/*     */     
/* 204 */     if (ascending) {
/* 205 */       return -1 * retVal;
/*     */     }
/* 207 */     return retVal;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/TaskHit.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */